package com.whjfxx.terminal.api.server.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whjfxx.terminal.biz.mapper.PoTerminalMapper;
import com.whjfxx.terminal.biz.service.PoTerminalService;
import com.whjfxx.terminal.utils.AddressUtil1;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class TerminalLocalAddressService {

    @Autowired
    PoTerminalMapper poTerminalMapper;

    @Async
    public void terminalLocalAddress(Long terminalId, String local) {
        if (StringUtils.isBlank(local) || Objects.isNull(terminalId)){
            return;
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(local);
            if (Objects.isNull(jsonObject.get("lon")) || Objects.isNull(jsonObject.get("lat"))) {
                return;
            }
            String address = AddressUtil1.getAddressNameByGeocoder1(jsonObject.getString("lon"), jsonObject.getString("lat"));
            if (StringUtil.isNotBlank(address)) {
                poTerminalMapper.setlocalAddress(terminalId, address);
            }
        } catch (Exception e) {
            log.warn("" ,e );
        }
    }

}
